﻿using Shop.Collect.Linq;
using Shop.Model.DB;
using Shop.RemoteModel.ActivityDiscount.Model;
using Shop.Repository;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;

namespace Shop.Collect.lmpl
{
    internal class ActivityDiscountCollect : IActivityDiscountCollect
    {
        private readonly IActivityDiscountRepository _Repository;

        public ActivityDiscountCollect (IActivityDiscountRepository repository)
        {
            this._Repository = repository;
        }

        public bool CheckIsExists (long activityId)
        {
            return this._Repository.IsExist(c => c.ActivityId == activityId);
        }

        public void Clear (long activityId)
        {
            long[] ids = this._Repository.Gets(a => a.ActivityId == activityId, a => a.Id);
            if (ids.IsNull())
            {
                return;
            }
            this._Repository.Delete(ids);
        }

        public DiscountSet Get (long activityId)
        {
            DBShopActivityDiscount[] list = this._Repository.Gets<DBShopActivityDiscount>(a => a.ActivityId == activityId);
            if (list.IsNull())
            {
                return null;
            }
            DBShopActivityDiscount one = list[0];
            return new DiscountSet
            {
                Threshold = one.Threshold,
                Items = list.ConvertMap<DBShopActivityDiscount, DiscountItem>()
            };
        }
        public void Sync (long activityId, DiscountSet discount)
        {
            string show = discount.GetDiscountShow();
            this._Repository.Sync(activityId, discount, show);
        }
    }
}
